package com.sykj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.pojo.Permission;
import com.sykj.pojo.Role;
import com.sykj.servies.PermissionServies;
import com.sykj.servies.RolePermissionServies;
import com.sykj.servies.RoleServies;
import com.sykj.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/config")
public class RoleController {
    //管理员角色
    @Autowired
    RoleServies roleServies;
    //权限对应的角色
    @Autowired
    RolePermissionServies rolePermissionServies;
    //权限
    @Autowired
    PermissionServies permissionServies;

    //查询
    @RequestMapping("/listRole")
    public String list(Model model, Page page){
        //调用分页插件**第几页开始显示。每页显示几条数据
        PageHelper.offsetPage(page.getStart(),page.getCount());
        List<Role> list=roleServies.list();
        //查询的数据传入，查出总条数
        int total = (int) new PageInfo<>(list).getTotal();
        page.setTotal(total);
        model.addAttribute("list",list);
        model.addAttribute("roleSize",total);

        /**
         * Map存放的是K,V键值对,K相当于主键,
         * 你put进去就相当于存了一条记录进去,再存同样的Key进去,就会更新.
         */
        Map<Role,List<Permission>> role_permissions = new HashMap<>();

        for (Role role : list) {
            //查出当前角色拥有哪些权限
            List<Permission> ps = permissionServies.list(role);
            role_permissions.put(role, ps);
        }
        //把当前角色的权限保存
        model.addAttribute("role_permissions", role_permissions);
        return  "syspage/admin-role";
    }

    //点击添加按钮，弹出子框，
    @RequestMapping("/addRoleUI")
    public String addRole(){

        return "syspage/admin-role-add";
    }
    //添加角色权限
    @RequestMapping("/addRole")
    public String list(Model model,Role role){
        roleServies.add(role);
        //重定向刷新
        return "redirect:listRole";
    }
    //设置角色能拥有的权限
    @RequestMapping("/updateRole")
    public String update(Role role,long[] permissionIds){
        // role 角色 permissionIds 权限id
        rolePermissionServies.setPermissions(role, permissionIds);
        roleServies.update(role);
        return "redirect:listRole";
    }
    //删除角色
    @RequestMapping("/deleteRole")
    public String delete(Model model,long id){
        roleServies.delete(id);
        //重定向刷新
        return "redirect:listRole";
    }
    @RequestMapping("/editRole")
    public String list(Model model,long id){
        //根据id拿到查询的结果
        Role role =roleServies.get(id);
        //将结果保存传到角色编辑页面
        model.addAttribute("role", role);
        //所有权限
        List<Permission> ps = permissionServies.list();
        model.addAttribute("ps", ps);
        //当前管理员拥有的权限
        List<Permission> currentPermissions = permissionServies.list(role);
        model.addAttribute("currentPermissions", currentPermissions);
        return "syspage/admin-role-edit";
    }
}
